package com.ruoyi.project.jxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.jxy.domain.Entry;
import com.ruoyi.project.jxy.service.IEntryService;
import com.ruoyi.project.jxy.service.ISlotEntityRefService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.Entity;
import com.ruoyi.project.jxy.service.IEntityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 实体与辅助词Controller
 *
 * @author ruoyi
 * @date 2019-12-05
 */
@RestController
@RequestMapping("/jxy/entity")
public class EntityController extends BaseController {
    @Autowired
    private IEntityService entityService;
    @Autowired
    private IEntryService entryService;
    @Autowired
    private ISlotEntityRefService slotEntityRefService;


    /**
     * 查询实体与辅助词列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:entity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Entity entity) {
        startPage();
        QueryWrapper<Entity> queryWrapper = new QueryWrapper<Entity>();
        //登陆人
        entity.setCreateUser(SecurityUtils.getUsername());
        List list = entityService.listEntity(entity);
        return getDataTable(list);
    }

    /**
     * 获取实体与辅助词详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxy:entity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Entity entityResult = entityService.getById(id);
        return AjaxResult.success(entityResult);
    }

    /**
     * 新增实体与辅助词
     */
    @PreAuthorize("@ss.hasPermi('jxy:entity:add')")
    @Log(title = "实体与辅助词", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Entity entity) {
        //校验该用户下是否存在该code
        String code = entity.getCode();
        QueryWrapper<Entity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("create_user", SecurityUtils.getUsername());
        queryWrapper.eq("code", code);
        int count = entityService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error("英文标识已存在");
        }
        //操作人
        entity.setCreateUser(SecurityUtils.getUsername());
        //操作时间
        entity.setCreateTime(new Date());
        entity.setRefTimes(0L);
        entity.setStatus("0");
        return toAjax(entityService.save(entity) ? 1 : 0);
    }

    /**
     * 修改实体与辅助词
     */
    @PreAuthorize("@ss.hasPermi('jxy:entity:edit')")
    @Log(title = "实体与辅助词", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Entity entity) {
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(SecurityUtils.getUsername());
        return toAjax(entityService.updateById(entity) ? 1 : 0);
    }

    /**
     * 删除实体与辅助词
     */
    @PreAuthorize("@ss.hasPermi('jxy:entity:remove')")
    @Log(title = "实体与辅助词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        //校验实体是否被引用
        for (Long id : ids) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("entity_id", id);
            List checkList = slotEntityRefService.list(wrapper);
            if (checkList != null && checkList.size() > 0) {
                Entity entity = entityService.getById(id);
                if (entity != null && entity.getRefTimes() > 0) {
                    String tempStr = entity.getType().equals("0") ? "实体" : "辅助词";
                    return AjaxResult.error("该" + tempStr + "被技能引用!请取消引用后再删除!");
                }
            }
        }
        return toAjax(entityService.removeEntity(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @return com.ruoyi.framework.web.domain.AjaxResult
     * @Description //获取用户的实体辅助词信息
     * @Date 2019/12/30 16:40
     * @Param [userName]
     **/
    @GetMapping("/listEntity")
    public AjaxResult listEntity(String userName) {
        List lst = entityService.listEntryInfo(userName);
        return AjaxResult.success(lst);
    }
}
